#include "MessageBoard.h"

MessageBoard::MessageBoard(void){
	MessageList.clear();
}

MessageBoard::~MessageBoard(void){
	for (vector< Message* >::iterator itr = MessageList.begin(); itr < MessageList.end(); ++itr)
		delete (*itr);
	MessageList.clear();
}

//void MessageBoard::Init(int MaxNumberOfMsg){
//	msg = new Message[MaxNumberOfMsg];
//	maxMsg = MaxNumberOfMsg;
//}

void MessageBoard::SetMessage(string message, Message::USER user, int id){
	if(SearchPost(user, id)){
		for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
			if((*itr)->Read() != message && (*itr)->GetID() == id && (*itr)->GetUser() == user){
				((*itr)->Post(message, user, id));
				return;
			}
		}
	}else{
		Message *tempMsg = new Message;
		tempMsg->Post(message, user, id);
		MessageList.push_back(tempMsg);
		return;
	}
}

void MessageBoard::SetMessage(string message, float value, Message::USER user, int id){
	if(SearchPost(user, id)){
		for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
			if((*itr)->GetValue1() != value && (*itr)->GetID() == id && (*itr)->GetUser() == user){
				((*itr)->Post(message, value, user, id));
				return;
			}
		}
	}else{
		Message *tempMsg = new Message;
		tempMsg->Post(message, value, user, id);
		MessageList.push_back(tempMsg);
		return;
	}
}

void MessageBoard::SetMessage(string message, float value1, float value2, Message::USER user, int id){
	if(SearchPost(user, id)){
		for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
			if((*itr)->GetValue1() != value1 || (*itr)->GetValue2() != value2 && (*itr)->GetID() == id && (*itr)->GetUser() == user){
				((*itr)->Post(message, value1, value2, user, id));
				return;
			}
		}
	}else{
		Message *tempMsg = new Message;
		tempMsg->Post(message, value1, value2, user, id);
		MessageList.push_back(tempMsg);
		return;
	}
}

void MessageBoard::ResetMessage(Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->GetID() == id && (*itr)->GetUser() == user){
			//cout << MessageList.size() << endl;
			itr = MessageList.erase(itr);
			//cout << MessageList.size() << endl;
			//delete (*itr);
			return;
		}
	}
}

void MessageBoard::ResetMessage(string message, Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->GetID() == id && (*itr)->GetUser() == user && (*itr)->Read() == message){
			//cout << MessageList.size() << endl;
			delete (*itr);
			MessageList.erase(itr);
			//cout << MessageList.size() << endl;
			//delete (*itr);
			return;
		}
	}
}

string MessageBoard::RetrieveMessage(Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->GetID() == id && (*itr)->GetUser() == user){
			return (*itr)->Read();
		}
	}

	return " ";
}

float MessageBoard::RetrieveFirstValueInMsg(Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->GetID() == id && (*itr)->GetUser() == user){
			return (*itr)->GetValue1();
		}
	}

	return 0;
}

float MessageBoard::RetrieveSecondValueInMsg(Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->GetID() == id && (*itr)->GetUser() == user){
			return (*itr)->GetValue2();
		}
	}

	return 0;
}

int MessageBoard::RetrieveID(string message, Message::USER user){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->Read() == message && (*itr)->GetUser() == user){
			return (*itr)->GetID();
		}
	}

	return -1;
}

bool MessageBoard::SearchPost(Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->GetID() == id && (*itr)->GetUser() == user){
			return true;
		}
	}

	return false;
}

bool MessageBoard::SearchPost(string message, Message::USER user){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->Read() == message && (*itr)->GetUser() == user){
			return true;
		}
	}

	return false;
}

bool MessageBoard::SearchPost(string message, Message::USER user, int id){
	for(vector<Message*>::iterator itr = MessageList.begin(); itr != MessageList.end(); ++itr){
		if((*itr)->Read() == message && (*itr)->GetUser() == user && (*itr)->GetID() == id){
			return true;
		}else if((*itr)->Read() == message && (*itr)->GetUser() == user && (*itr)->GetID() != id){
			return false;
		}
	}

	return false;
}